//
//  HomeView.swift
//  JonCalculator
//
//  Created by shiyanjun on 2023/1/13.
//

import SwiftUI

struct HomeView: View {
    // 按钮二维数组
    let buttons: [[CalcButton]] = [
        [.clear, .negative, .percent, .divide],
        [.seven, .eight, .nine, .mutliply],
        [.four, .five, .six, .subtract],
        [.one, .two, .three, .add],
        [.zero, .decimal, .equal],
    ]
    
    @ObservedObject var model: ViewModel = ViewModel()
    
    var body: some View {
        ZStack {
            // 背景
            Color.black
                .ignoresSafeArea(.all)
            
            // 使用Grid布局
            Grid(horizontalSpacing: 20, verticalSpacing: 10) {
                // 显示屏
                GridRow {
                    ZStack(alignment: .top) {
                        Rectangle()
                            .foregroundColor(.cyan)
                            .frame(maxWidth: .infinity, maxHeight: 200)
                            .cornerRadius(25)
                            .overlay(alignment: .bottomTrailing) {
                                Text("\(model.displayValue)")
                                    .font(.system(size: 70))
                                    .padding(.trailing, 10)
                                    .padding(.bottom, 10)
                            }
                            
                    }
                    .gridCellColumns(4)
                }
                
                // 按键
                ForEach(buttons, id: \.self) { row in
                    GridRow {
                        ForEach(row, id: \.self) { item in
                            Button {
                                model.onButtonPressed(button: item)
                            } label: {
                                Text(item.rawValue)
                                    .font(.system(size: 25, weight: .bold))
                                    .foregroundColor(.white)
                                    .frame(maxWidth: .infinity)
                                    .frame(height: 70)
                                    // 设置按钮颜色
                                    .background(buttonColor(button: item))
                                    .cornerRadius(25)
                            }
                            // 按键0占据2个格子
                            .gridCellColumns(item.rawValue == "0" ? 2 : 1)
                        }
                    }
                }
                
            }
            .frame(maxWidth: UIScreen.main.bounds.width - 35)
        }
    }
    
    // 设置按钮颜色
    func buttonColor(button: CalcButton) -> Color {
        switch button {
        case .add, .subtract, .mutliply, .divide, .equal:
            return .orange
        case .clear:
            return .red
        case .negative, .percent:
            return Color(.lightGray)
        default:
            return Color(UIColor(red: 55/255.0, green: 55/255.0, blue: 55/255.0, alpha: 1))
        }
    }
}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}
